package com.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.druid.support.json.JSONWriter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.mypg.Info;
import com.mypg.InfoHelper;
import com.mypg.News;
import com.service.InfoService;
import com.service.NewsService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.swing.plaf.LayerUI;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/test")
@Controller
public class TestController {

    @Autowired
    UserService userService;
    @Autowired
    InfoService infoService;
    @Autowired
    NewsService newsService;

    @RequestMapping("/info_table_test.do")
    @ResponseBody
    public List<Info> doinfo_table_test() throws ParseException {
        String time1="2020-01-01 01:01";
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Timestamp timestamp1=new Timestamp(df.parse(time1).getTime());
        Timestamp timestamp2=new Timestamp(System.currentTimeMillis());
        List<Info> infoList=infoService.selectInfoForinfo_table_jsp("新手",timestamp1,timestamp2,0,5);
        System.out.println(infoList);
        return infoList;
//       return infoService.selectInfoByinfo_id(1);
    }

    @RequestMapping("/info_table_test_insert.do")
    public ModelAndView doInfo_table_test_insert(HttpServletRequest request){
        ModelAndView mv=new ModelAndView();
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        System.out.println(timestamp);
        Info info=new Info();
        info.setInfo_title("标题");
        info.setInfo_content("内容");
        info.setInfo_time(timestamp);
        info.setInfo_class("新手学车");
        infoService.insertInfo(info);
        mv.setViewName("redirect:/test/test2.jsp");
        return mv;
    }

    @RequestMapping("/table_split_test.do")
    @ResponseBody
    public String doTable_split_test(String info_class,String start_time,String end_time,int page,int limit) throws ParseException, JsonProcessingException {

        HashMap<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",infoService.selectInfo_count());
        int start_row=(page-1)*limit;
        List<Info> infoList=changeRows(info_class,start_time,end_time,start_row,limit);
        List<InfoHelper> infoHelperList=InfoHelper.changeInfolistToInfoHelperlist(infoList);
        map.put("data",infoHelperList);
        System.out.println(infoHelperList);
        ObjectMapper mapper=new ObjectMapper();
        String map_json=mapper.writeValueAsString(map);
        System.out.println(map_json);
        return map_json;
    }


    @RequestMapping("/news_table_select_all_test.do")
    @ResponseBody
    public List<News> doNews_table_select_all_test(){
        return newsService.selectAllNews();
    }

    @RequestMapping("/news_table_insert_test.do")
    @ResponseBody
    public void doNews_table_insert_test(){
        Timestamp timestamp=new Timestamp(System.currentTimeMillis());
        String news_title="新闻";
        int count=newsService.selectNews_count()+1;
        news_title+=count;
        News news=new News();
        news.setNews_title(news_title);
        news.setNews_content("内容");
        news.setNews_img("news_img_default.jpg");
        news.setNews_time(timestamp);
        news.setNews_class("使用技巧");
        newsService.insertNews(news);
    }

    @RequestMapping("/news_table_delete_test.do")
    @ResponseBody
    public void doNews_table_delete_test(){
        newsService.deleteNewsByNews_id(5);
    }

    @RequestMapping("/news_table_update_test.do")
    @ResponseBody
    public void doNews_table_update_test(){
        News news=new News();
        news.setNews_id(6);
        news.setNews_content("修改内容");
        newsService.updateNewsByNews_id(news);
    }

    @RequestMapping("/news_table_select_test.do")
    @ResponseBody
    public List<News> doNews_table_select_test(String news_class,String time_start,String time_end,
                                               int start_row,int page_nums) throws ParseException {
        Timestamp timestamp_start;
        Timestamp timestamp_end;
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if(time_start==null||time_start==""){
            time_start="1970-01-01 01:01";
            timestamp_start=new Timestamp(df.parse(time_start).getTime());
        }else{
            timestamp_start=new Timestamp(df.parse(time_start).getTime());
        }

        if(time_end==null||time_end==""){
            timestamp_end=new Timestamp(System.currentTimeMillis());
        }else{
            timestamp_end=new Timestamp(df.parse(time_end).getTime());
        }
        return newsService.selectNewsFor_news_table_jsp(news_class,timestamp_start,timestamp_end,start_row,page_nums);
    }

    @RequestMapping("/upload_file_test.do")
    @ResponseBody
    public String doUpload_file_test(@RequestParam(value="file",required = false) MultipartFile file,
                                     String name) throws JsonProcessingException {
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",file.getName());
        System.out.println(name);
        System.out.println(file.getName());
        System.out.println(file.getResource());
        System.out.println(file.getOriginalFilename());
        return new ObjectMapper().writeValueAsString(map);
    }





























    public List<Info> changeRows(String info_class,String time_start,String time_end,
                                 int start_row,int page_nums) throws ParseException {
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Timestamp timestamp_start=null;
        Timestamp timestamp_end=null;
        if(time_start!=null&&time_end!=null) {
            if ("".equals(time_start)) {
                time_start = "1970-01-01 01:01";
                timestamp_start = new Timestamp(df.parse(time_start).getTime());
            } else {
                time_start = time_start.replace("T", " ");
                timestamp_start = new Timestamp(df.parse(time_start).getTime());
            }
            if ("".equals(time_end)) {
                timestamp_end = new Timestamp(System.currentTimeMillis());
            } else {
                time_end = time_end.replace("T", " ");
                timestamp_end = new Timestamp(df.parse(time_end).getTime());
            }
        }

        List<Info> infoList = infoService.selectInfoForinfo_table_jsp(info_class,timestamp_start,timestamp_end,start_row,page_nums);
        return infoList;
    }
}
